package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddLog;
import com.baizhi.dao.AdminMapper;
import com.baizhi.entity.Admin;
import com.baizhi.entity.AdminExample;
import com.baizhi.entity.Feedback;
import com.baizhi.service.AdminService;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    @Resource
    HttpServletRequest request;

    @Override
    public HashMap<String, Object> login(Admin admin, String enCode) {
        HashMap<String, Object> map = new HashMap<>();
        //1.获取验证码
        String code = (String) request.getServletContext().getAttribute("code");

        //2.判断验证码是否一致
        if(code.equals(enCode)){
            //3.判断用户是否存在
            Admin admins = adminMapper.queryByUsername(admin.getUsername());
            if(admins!=null){
                //4.判断用户是否正常
                if(admins.getStatus().equals("1")){
                    //5.判断密码是否正确
                    if(admin.getPassword().equals(admins.getPassword())){

                        request.getServletContext().setAttribute("admin",admins);

                        map.put("message",admins);
                        map.put("status",200);
                    }else{
                        map.put("message","用户密码错误");
                        map.put("status",400);
                    }
                }else{
                    map.put("message","用户处于冻结状态，请联系超级管理员");
                    map.put("status",400);
                }
            }else{
                map.put("message","用户不存在");
                map.put("status",400);
            }
        }else{
            map.put("message","验证码错误");
            map.put("status",400);
        }
        return map;
    }

    @Override
    public HashMap<String, Object> queryAllpage(Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();

        map.put("page",page);

        AdminExample example = new AdminExample();

        int count = adminMapper.selectCountByExample(example);

        map.put("total",count);

        //创建分页对象  参数：起始条数，数据数
        RowBounds bounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        List<Admin> admins = adminMapper.selectByExampleAndRowBounds(example, bounds);

        //设置查询数据
        map.put("rows",admins);

        return map;
    }

    @AddLog(value = "修改管理员")
    @Override
    public HashMap<String, Object> update(Admin admin) {

        HashMap<String, Object> map = new HashMap<>();

        try {
            adminMapper.updateByPrimaryKeySelective(admin);
            map.put("message","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败");
        }

        return map;
    }

    @AddLog(value = "删除管理员")
    @Override
    public HashMap<String, Object> delete(Admin admin) {

        HashMap<String, Object> map = new HashMap<>();

        try {
            adminMapper.delete(admin);
            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }

        return map;
    }

    @AddLog(value = "添加管理员")
    @Override
    public CommonVo add(Admin admin) {
        try {
            admin.setId(UUIDUtil.getUUID());
            admin.setStatus("1");

            adminMapper.insertSelective(admin);

            return CommonVo.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVo.faild("添加失败");
        }
    }

    @Override
    public Admin queryById(String id) {
        return adminMapper.selectByPrimaryKey(id);
    }

}
